package com.sf.dlb.service;

import com.sf.dlb.bean.ETHWalletResponseBean;
import com.sf.dlb.jpa.model.WalletEthAddressInfo;
import com.sf.dlb.jpa.repository.WalletEthAddressRepository;
import com.sf.dlb.config.Global;
import com.sf.dlb.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * 用户eth地址表 service
 *
 * Created by jing.tan on 2018/5/21.
 */
@Service
public class WalletEthAddressService extends BaseWalletEthAddressService{
    private Logger logger= LoggerFactory.getLogger(WalletEthAddressService.class);
    @Autowired
    private WalletEthAddressRepository walletEthAddressRepository;
    @Autowired
    private ETHWalletService ethWalletService;
    @Resource
    private ExecutorService executorService;
    @Autowired
    private Global global;

    /**
     * 绑定用户与eth地址
     * @return
     */
    @Transactional
    public void banddingUserAndAddress(String userId){
        logger.info("banddingUserAndAddress userId={} ",userId);
        WalletEthAddressInfo walletEthAddressInfo=walletEthAddressRepository.getOneNotBandingAddress();
        if(null==walletEthAddressInfo){
            logger.info("banddingUserAndAddress  null==walletEthAddressInfo");
            return;
        }
        walletEthAddressInfo.setUserId(userId);
        walletEthAddressInfo.setUpdateDate(new Date());
        walletEthAddressRepository.save(walletEthAddressInfo);
        logger.info("banddingUserAndAddress walletEthAddressInfo={} ", GsonUtils.getInstance().toJson(walletEthAddressInfo));
        //是否需要生成eth地址
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                handlerGenerateAddress();
            }
        });

    }
    /**
     * 处理eth地址的生成
     * @return
     */
    public void handlerGenerateAddress(){
       Integer addressCount=walletEthAddressRepository.notBandingAddressCount();
       int remainNumber=global.py_address_remain_number_threshold;//剩余eth地址数量阈值，超过了，就需要py去生成
        logger.info("handlerGenerateAddress addressCount={},remainNumber={}",addressCount,remainNumber);
       if(addressCount<=remainNumber){
           List<ETHWalletResponseBean> ethWalletResponseBeans=ethWalletService.generateAddress();
           if(CollectionUtils.isEmpty(ethWalletResponseBeans)){
               logger.info("handlerGenerateAddress ethWalletResponseBeans is null");
               return;
           }
           List<WalletEthAddressInfo> walletEthAddressInfos=new ArrayList<>();
           for (ETHWalletResponseBean ethWalletResponseBean:ethWalletResponseBeans) {
               WalletEthAddressInfo walletEthAddressInfo=new WalletEthAddressInfo();
               walletEthAddressInfo.setAddress(ethWalletResponseBean.getAddress());
               walletEthAddressInfo.setPrivateKey(ethWalletResponseBean.getPrivateKey());
               walletEthAddressInfo.setPublicKey(ethWalletResponseBean.getPublicKey());
               walletEthAddressInfo.setUserQc(ethWalletResponseBean.getAddress());
               walletEthAddressInfo.setCreateDate(new Date());
               walletEthAddressInfo.setUpdateDate(new Date());
               walletEthAddressInfos.add(walletEthAddressInfo);
           }
           logger.info("handlerGenerateAddress ethWalletResponseBeans={},walletEthAddressInfos={}",ethWalletResponseBeans.size(),walletEthAddressInfos.size());
           walletEthAddressRepository.save(walletEthAddressInfos);
       }
    }
}